package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.ElderDTO;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.ElderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 老人Service业务层处理
 *
 * @author mr.luo
 * @date 2025-05-17
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     *
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人列表
     *
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return save(elder) ? 1 : 0;
    }

    /**
     * 修改老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return updateById(elder) ? 1 : 0;
    }

    /**
     * 批量删除老人
     *
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人信息
     *
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询已经入住的老人列表
     *
     * @param elderDTO
     * @return
     */
    /*
    status
pageNum
pageSize
name
idCardNo
    */
    @Override
    public Page<ElderVO> pageQuery(ElderDTO elderDTO) {
        Page<Elder> page = new Page<>(elderDTO.getPageNum(), elderDTO.getPageSize());
//        page.addOrder(OrderItem.desc("create_time"));
        lambdaQuery().eq(elderDTO.getStatus() != null, Elder::getStatus, elderDTO.getStatus())
                .like(!StrUtil.isBlank(elderDTO.getName()), Elder::getName, elderDTO.getName())
                .eq(!StrUtil.isBlank(elderDTO.getIdCardNo()), Elder::getIdCardNo, elderDTO.getIdCardNo())
                .orderByDesc(Elder::getCreateTime).page(page);
        List<ElderVO> collect = page.getRecords().stream().map(s -> {
            ElderVO elderVO = new ElderVO();
            BeanUtils.copyProperties(s, elderVO);
            return elderVO;
        }).collect(Collectors.toList());
        Page<ElderVO> page1 = new Page<>();
        page1.setRecords(collect);
        page1.setTotal(page.getTotal());
        return page1;
    }
}
